<?php
require_once 'App/AlbumService.php';
require_once 'App/UserService.php';
require_once 'App/ImageService.php';
require_once 'Zend/Controller/Action.php';
require_once 'App/Forms/AlbumSelector.php';

class AlbumController extends Zend_Controller_Action
{
	protected $albumService;
	protected $userService;
	protected $imageService;
	
	public function preDispatch()
	{
		$this->albumService = new App_AlbumService();
		$this->userService = new App_UserService();
		$this->imageService = new App_ImageService();
		$this->session = new Zend_Session_Namespace('default');
		$layout = Zend_Layout::getMvcInstance();
		$layout->message = '';
	}
	
    public function init()
    {
        /* Initialize action controller here */
    }

    private function showDefault()
    {
    	$userID = $this->session->userID;
     	$this->view->allAlbums = $this->albumService->getAllAlbumsByUserID($userID);
     	$albumID = $this->albumService->getDefaultAlbumID($userID);
     	$images = $this->imageService->getPicsFromAlbumByID($albumID);
     	$this->view->picsArray = $images;
    }
    
    public function indexAction()
    {
        //Authenticate user
     	if ($this->_request->isPost())
     	{
     		$data = $this->getRequest()->getPost();

     		if(isset($data['username'])) {
	     		$user = $this->userService->getUser($data['username']);
	     		
	     		if ($user->Password == $data['password'])
	     		{
	     			$userID = $user->UserID;
	     			$this->session->userID = $userID;
	     			$this->session->userName = $data['username'];
	     			
	     			//Display user albums, etc...
	     		}
	     		else 
	     		{
	     			$this->session->loginFail = true;
	     			$this->_redirect('index');
	     		}
	     		
	     		$this->showDefault();
     		}
     		elseif (isset($data['albumID']))
     		{
     			$albumID = $this->getRequest()->getParam('albumid');
	    		$images = $this->imageService->getPicsFromAlbumByID($data['albumID']);
	    		$this->view->picsArray = $images;
	    
	     		$form = new App_Forms_AlbumSelector();
	     		$this->view->form = $form;		
     		}
     		elseif(isset($data['searchAction']))
     		{
     			$nameArr = array();
				$valArr = array();
				$nameCount = 0;
				$valCount = 0;
				$bad_data = false;
	     		foreach($data as $key => $value)
				{
					if($value == '')
					{
						$bad_data = true;
						break;
					}
					if($key == 'searchAction')
					{
						//ignore it
					}
					else
					{
						$type = substr($key, 0, 4);
						
						if ($type == 'name')
						{
							$nameArr[$nameCount++] = $value;
						}
						if ($type == 'valu')
						{
							$valArr[$valCount++] = $value;
						}
					}
				}
				
				
				// OR the attributes
				$images = $this->imageService->getPicsByAttributes($nameArr, $valArr);
				//$images = $this->imageService->getPicsByAttributes($nameArr, $valArr);
				//$images = $this->imageService->getPicsByAttr($nameArr[0], $valArr[0]);
     			//$images = $this->imageService->getPicsByAttr($data['name'], $data['value']);
     			$this->view->picsArray = $images;
     		}
     	}
     	elseif (isset($this->session->viewAlbumID))
     	{
     		$images = $this->imageService->getPicsFromAlbumByID($this->session->viewAlbumID);
     		$this->view->picsArray = $images;
     		unset($this->session->viewAlbumID);
     	}
    	elseif (isset($this->session->userID))
    	{
    		$this->showDefault();
    	}
    	
     	$form = new App_Forms_AlbumSelector();
     	$this->view->form = $form;
    }

    public function addAction()
    {	
    	$form = new App_Forms_AlbumForm();
    	$this->view->form = $form;
    	
    	if ($this->_request->isPost()) 
    	{
    		$postData = $this->getRequest()->getPost();
    		$this->albumService->saveAlbum($postData['name'], $postData['description']);
    		
    		$album_id = $this->albumService->getLastID();
    		
    		$upload = new Zend_File_Transfer_Adapter_Http();
    		$upload->setDestination('..\user_data');
    		
    		try{
    			$upload->receive();
    		}
    		catch(Zend_File_Transfer_Exception $e)
    		{
    			throw new Exception('Bad image data: '. $e->getMessage());
    		}
    		
	    	$filePaths = $upload->getFileName('imageFile');
			
			foreach($filePaths as $fileName)
			{
				$this->imageService->saveImage($album_id, $fileName);	
			}

			$this->_helper->viewRenderer->setNoRender(true);
			$this->_redirect('album');
    	}
    }
    
    public function showAction()
    {
    	if($this->getRequest()->getParam('albumid'))
    	{
	    	$albumID = $this->getRequest()->getParam('albumid');
	    	$images = $this->imageService->getPicsFromAlbumByID($albumID);
	    	$this->view->picsArray = $images;
	    
	     	$form = new App_Forms_AlbumSelector();
	     	$this->view->form = $form;    	
    	}
    }
}

